enr
This crate contains an implementation of an Ethereum Node Record (ENR) as specified by EIP-778 extended to allow for the use of ed25519 keys.
An ENR is a signed, key-value record which has an associated NodeId
(a 32-byte identifier).
Updating/modifying an ENR requires an EnrKey
in order to re-sign the record with the
associated key-pair.
ENR's are identified by their sequence number. When updating an ENR, the sequence number is increased.
Different identity schemes can be used to define the node id and signatures. Currently only the "v4" identity is supported and is set by default.
Signing Algorithms
User's wishing to implement their own signing algorithms simply need to
implement the EnrKey
trait and apply it to an Enr
.
By default, k256::SigningKey
implement EnrKey
and can be used to sign and
verify ENR records. This library also implements EnrKey
for ed25519_dalek::Keypair
via the ed25519
feature flag.
Furthermore, a CombinedKey
is provided if the ed25519
feature flag is set, which provides an
ENR type that can support both secp256k1
and ed25519
signed ENR records. Examples of the
use of each of these key types is given below.
Features
This crate supports a number of features.
serde
: Allows for serde serialization and deserialization for ENRs.ed25519
: Provides support fored25519_dalek
keypair types.rust-secp256k1
: Usesc-secp256k1
for secp256k1 keys.
These can be enabled via adding the feature flag in your Cargo.toml
= { = "*", = ["serde", "ed25519", "rust-secp256k1"] }
Examples
Building an ENR with the default k256
key type
use ;
use Ipv4Addr;
use thread_rng;
// generate a random secp256k1 key
let mut rng = thread_rng;
let key = random;
let ip = new;
let enr = builder.ip4.tcp4.build.unwrap;
assert_eq!;
assert_eq!;
Building an ENR with the CombinedKey
type (support for multiple signing algorithms).
Note the ed25519
feature flag must be set. This makes use of the
builder::Builder
struct.
use ;
use Ipv4Addr;
// create a new secp256k1 key
let key = generate_secp256k1;
// or create a new ed25519 key
let key = generate_ed25519;
let ip = new;
let enr = builder.ip4.tcp4.build.unwrap;
assert_eq!;
assert_eq!;
Modifying an ENR
ENR fields can be added and modified using the getters/setters on Enr
. A custom field
can be added using insert
and retrieved with get
.
use ;
use Ipv4Addr;
use thread_rng;
// specify the type of ENR
type DefaultEnr = ;
// generate a random secp256k1 key
let mut rng = thread_rng;
let key = random;
let ip = new;
let mut enr = builder.ip4.tcp4.build.unwrap;
enr.set_tcp4;
// set a custom key
enr.insert;
// encode to base64
let base_64_string = enr.to_base64;
// decode from base64
let decoded_enr: DefaultEnr = base_64_string.parse.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Encoding/Decoding ENR's of various key types
use ;
use Ipv4Addr;
use thread_rng;
use Rng;
// generate a random secp256k1 key
let mut rng = thread_rng;
let key = random;
let ip = new;
let enr_secp256k1 = builder.ip4.tcp4.build.unwrap;
// encode to base64
let base64_string_secp256k1 = enr_secp256k1.to_base64;
// generate a random ed25519 key
let mut rng = thread_rng;
let key = generate;
let enr_ed25519 = builder.ip4.tcp4.build.unwrap;
// encode to base64
let base64_string_ed25519 = enr_ed25519.to_base64;
// decode base64 strings of varying key types
// decode the secp256k1 with default Enr
let decoded_enr_secp256k1: = base64_string_secp256k1.parse.unwrap;
// decode ed25519 ENRs
let decoded_enr_ed25519: = base64_string_ed25519.parse.unwrap;
// use the combined key to be able to decode either
let decoded_enr: = base64_string_secp256k1.parse.unwrap;
let decoded_enr: = base64_string_ed25519.parse.unwrap;